package com.xfcycle.zebrabikegp.activity;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xfcycle.zebrabikegp.R;
import com.xfcycle.zebrabikegp.constant.BluetoothDeviceConstant;
import com.xfcycle.zebrabikegp.util.HandleDataByBLEUtil;
import com.xfcycle.zebrabikegp.util.HandleDataBySharePreferencesUtil;

/**
 * 蓝牙钥匙设置
 */
public class BsTestActivity extends RootActivity {
    private static final String TAG = "BsTestActivity";

    private ImageView iv_bike_test_back;

    private Button btn_bike_test_brake;

    private RelativeLayout rl_cover;
    private TextView tv_test_result;

    private HandleDataByBLEUtil handleDataByBLEUtil;
    private HandleDataBySharePreferencesUtil handleDataBySharePreferencesUtil;

    private BikeTestHandleDataFromBleReceiver bikeTestHandleDataFromBleReceiver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bs_test);

        initView();
        initViewListener();
        setViewContent();
    }

    @Override
    protected void onResume() {
        super.onResume();

        //注册广播
        bikeTestHandleDataFromBleReceiver = new BikeTestHandleDataFromBleReceiver();
        registBroadcastReceiverAtActivity(bikeTestHandleDataFromBleReceiver, BluetoothDeviceConstant.GET_DATA_FROM_BLUETOOTH_BROADCAST_RECEIVER_ACTION);
    }

    @Override
    protected void onPause() {
        super.onPause();
        //回收广播接收者
        unregisterReceiver(bikeTestHandleDataFromBleReceiver);
    }
    /***********************************************************************************************/
    /**                                 初始化控件对象 监听 控件内容方法                             **/
    /***********************************************************************************************/

    private void initView() {
        iv_bike_test_back = (ImageView) findViewById(R.id.iv_bike_test_back);
        rl_cover = (RelativeLayout) findViewById(R.id.rl_cover);

        tv_test_result = (TextView) findViewById(R.id.tv_test_result);

        btn_bike_test_brake = (Button) findViewById(R.id.btn_bike_test_brake);

        handleDataByBLEUtil = HandleDataByBLEUtil.getInstance(this, -1);
        handleDataBySharePreferencesUtil = HandleDataBySharePreferencesUtil.getInstance(this);
    }

    private void initViewListener() {
        //回退事件监听
        iv_bike_test_back.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                closeActivity();
            }
        });

        //刹车自检
        btn_bike_test_brake.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rl_cover.setVisibility(View.VISIBLE);

                handleDataByBLEUtil.sendDataToDevice("AT+BV");

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        SystemClock.sleep(5*1000);

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if(View.VISIBLE == rl_cover.getVisibility()){
                                    rl_cover.setVisibility(View.GONE);
                                }
                            }
                        });
                    }
                }).start();
            }
        });
    }

    private void setViewContent() {
    }

    /***********************************************************************************************/
    /**                             处理从不同途径获得的数据                                         **/
    /***********************************************************************************************/
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                //获得 扫描二维码 得到蓝牙钥匙名称
                case REQUEST_CODE_FIND_BIKE_QRCODE:

                    break;
            }
        }
    }

    /***********************************************************************************************/
    /**                                 自定义广播接收者类                                          **/
    /***********************************************************************************************/

    /**
     * 广播接收者类，负责接收从蓝牙发出的数据并对其进行处理
     */
    class BikeTestHandleDataFromBleReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            final String result = intent.getStringExtra("result");

            Log.i(TAG, "onReceive: 获取到的结果: " + result);

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    tv_test_result.setText(result);
                }
            });

            rl_cover.setVisibility(View.GONE);
        }
    }
}
